package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.dto.ProDto;
import com.example.dto.ProNormDto;
import com.example.service.ProNormService;
import com.example.service.ProService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("proNorm")
public class ProNormController {
    @Autowired
    private ProNormService service;

    @RequestMapping("add")
    public Object add(@RequestBody ProNormDto dto) {
        dto.setCreateTime(LocalDateTime.now());
        String[] valueList = dto.getValueList();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < valueList.length; i++) {
            if (i > 0) {
                sb.append("，");
            }
            sb.append(valueList[i]);
        }
        dto.setValue(sb.toString());
        System.out.println(dto);
        service.add(dto);
        return null;
    }

    @RequestMapping("select")
    public IPage<ProNormDto> select(@RequestBody ProNormDto dto) {
        IPage<ProNormDto> page = service.select(dto);
        List<ProNormDto> list = page.getRecords();
        for (ProNormDto proNormDto : list) {
            String norm = proNormDto.getValue();
            proNormDto.setValueList(norm.split("[，,]"));
        }
        return page;
    }


    @RequestMapping("selectName")
    public List<Map<String,Object>> selectNormValue() {
        List<Map<String,Object>>listmap = service.selectName();
        System.out.println(listmap);
        return listmap;
    }

    @RequestMapping("update")
    public Object update(@RequestBody ProNormDto dto) {
        String[] valueList = dto.getValueList();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < valueList.length; i++) {
            if (i > 0) {
                sb.append("，");
            }
            sb.append(valueList[i]);
        }
        dto.setValue(sb.toString());
        service.update(dto);
        return null;
    }

    @RequestMapping("delete")
    public Object delete(@RequestBody ProNormDto dto) {

        service.delete(dto);
        return null;
    }


}
